# coding:utf-8
'''
Created on 2015年6月29日

@author: Emrys
'''


class Structure:
    '''
        功能主要是给实例添加一些属性
    '''
    _fields = []

    def __init__(self, *args, **kwargs):
        if len(args) != len(self._fields):
            raise TypeError('Excepted {}  arguments'.format(len(self._fields)))

        for name, value in zip(self._fields, args):
            setattr(self, name, value)

        # 这里使用dict_key - list获得的元素属于dict_key但是不属于_fields
        # 具体查看Dictionary view objects
        # 不过这些运算只对keys有用，可以通过dir(dict.keys())看有无集合的运算
        extra_args = kwargs.keys() - self._fields
        for name in extra_args:
            setattr(self, name, kwargs.pop(name))
        if kwargs:
            raise TypeError('Duplicate values for {}'.format(','.join(kwargs)))


if __name__ == '__main__':
    class Stock(Structure):
        _fields = ['name', 'shares', 'price']

    s1 = Stock('Acme', 50, 91.1)
    s2 = Stock('Acme', 50, 91.1, day=1, month=2, year=5)
    print(dir(s1))
    print(dir(s2))
